package com.china08.yxyapi.api.comm;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class BookController extends YxyApiBaseController {

    @Autowired
    private YxyResBookRepository yxyResBookRepository;

    @ApiOperation(value = "获得课本")
    @RequestMapping(value = "/api_yw/comm/book", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(@RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();
        YxyResBook resBook = yxyResBookRepository.findById(reqModel.book.trim());
        if (resBook != null) {
            model.phaseId = resBook.getPhaseId();
            model.subjectId = resBook.getSubjectId();
            model.versionId = resBook.getVersionId();
            model.semersterId = resBook.getId();
        }
        model.setMessage("成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String book;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(book, null), "请填写课本ID");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        public String phaseId;
        public String subjectId;
        public String versionId;
        public String semersterId;
    }

}
